﻿//   EditContactInfo.aspx.cs
//   Copyright (c) 2012 Eswar Prakash
// 
//    This library is free software; you can redistribute it and/or modify  
//    it  under the terms of the GNU Lesser General Public License version  
//    2.1 as published by the Free Software Foundation.                     
//                                                                          
//    This library is distributed in the hope that it will be useful, but   
//    WITHOUT ANY WARRANTY; without even the implied warranty of            
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     
//    Lesser General Public License for more details.                       
//                                                                          
//    You should have received a copy of the GNU Lesser General Public      
//    License along with this library; if not, write to the Free Software   
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  
//    USA
namespace CKSAS.Samples.AppStorage.ApplicationPages
{
    using System;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using CKSAS.AppStorage;

    /// <summary>
    /// Implements the code behind for EditContactInfo.aspx
    /// </summary>
    public partial class EditContactInfoPage : LayoutsPageBase
    {
        /// <summary>
        /// Represents that method that handles the <see cref="E:System.Web.UI.Control.Load"/> event of the page.
        /// </summary>
        /// <param name="e">A <see cref="T:System.EventArgs"/> that contains the event data.</param>
        protected override void OnLoad(EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                var manager = new AppStorageManager(this.Web);
                var currentSettings = manager.GetObject<ContactSettings>("ContactSettings");
                if (currentSettings != null)
                {
                    this.textFullName.Text = currentSettings.FullName;
                    this.textEmailAddress.Text = currentSettings.EmailAddress;
                    this.textMobileNumber.Text = currentSettings.MobileNumber;
                    this.textTelephoneNumber.Text = currentSettings.TelephoneNumber;
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the btnOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                var manager = new AppStorageManager(this.Web);
                var currentSettings = manager.GetObject<ContactSettings>("ContactSettings");
                if (currentSettings == null)
                {
                    currentSettings = new ContactSettings(manager);
                }

                currentSettings.FullName = this.textFullName.Text;
                currentSettings.EmailAddress = this.textEmailAddress.Text;
                currentSettings.TelephoneNumber = this.textTelephoneNumber.Text;
                currentSettings.MobileNumber = this.textMobileNumber.Text;

                currentSettings.Update();

                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Close_Window",
                                                             "window.frameElement.commitPopup();", true);
            }
            catch
            {
                
            }
        }
    }
}